package com.noble.meether.ui.invite;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.utils.ScreenUtils;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.ui.invite.adapter.InviteDeclareAdapter;
import com.noble.meether.ui.invite.bean.InviteDeclareList;
import com.tencent.tauth.Tencent;

import org.json.JSONObject;

/**
 * 邀请赚钱
 */
public class InviteAct extends BaseAppCompatActivity implements View.OnClickListener, RequestComplete {
    private TextView tvInviteSum;
    private RecyclerView rlvList;
    private RelativeLayout btn_exchange;
    private InviteDeclareAdapter declareAdapter;
    private float toDpMutliple = 1; //根据屏幕密度获取屏幕转换倍数

    private UserDetail userDetail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_invite_money);
        initView();
    }

    @Override
    protected boolean isStatusBarLight() {
        return false;
    }

    private void initTitle() {
        setTitleBackground(getResources().getColor(R.color.transparent));
        setBackView(false);
        setTitle("邀请赚钱", getResources().getColor(R.color.white));
        setTitleRight("邀请明细", getResources().getColor(R.color.white), this);
    }

    private void initView() {
        initTitle();
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        toDpMutliple = ScreenUtils.toDpMultiple(this);
        btn_exchange = findViewById(R.id.btn_exchange);
        if (!userDetail.isMan()) {
            btn_exchange.setVisibility(View.GONE);
        }
        btn_exchange.setOnClickListener(this);
        findViewById(R.id.invite_declare).setOnClickListener(this);
        findViewById(R.id.btn_apply).setOnClickListener(this);
        findViewById(R.id.btn_share).setOnClickListener(this);
        tvInviteSum = findViewById(R.id.tv_money);
        rlvList = findViewById(R.id.recycler_view);
        rlvList.setLayoutManager(new LinearLayoutManager(this));
        rlvList.addItemDecoration(new ItemSpaces((int) (10 * toDpMutliple)));
        declareAdapter = new InviteDeclareAdapter(this);
        rlvList.setAdapter(declareAdapter);
        tvInviteSum.setText(String.valueOf(userDetail.getAmountInvite()));

        ModuleMgr.getCenterMgr().reqMyPartInfo(new String[]{"AmountInvite"}, this);
        ModuleMgr.getCommonMgr().reqInviteDeclare(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.invite_declare:
                MeetUIShow.showInviteRuleAct(this);
                break;
            case R.id.base_title_right_txt:
                MeetUIShow.showInviteDetailAct(this);
                break;

            case R.id.btn_exchange:
                MeetUIShow.showInviteExchangeAct(this);
                break;

            case R.id.btn_apply:
                MeetUIShow.showApplyAct(this, -2);
                break;

            case R.id.btn_share:
                MeetUIShow.showShareDialog(this);
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqInviteDeclare) {
            if (!response.isOk()) return;

            InviteDeclareList list = (InviteDeclareList) response.getBaseData();
            if (list.getInviteDeclareList().size() <= 0) return;
            declareAdapter.setList(list.getInviteDeclareList());
            return;
        }

        if (response.getUrlParam() == UrlParam.reqMyInfo) {
            if (!response.isOk()) return;

            JSONObject jsonObject = response.getResponseObject();
            userDetail.setAmountInvite(jsonObject.optDouble("AmountInvite"));
            tvInviteSum.setText(String.valueOf(userDetail.getAmountInvite()));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, null);
    }

    private class ItemSpaces extends RecyclerView.ItemDecoration {
        private int space;

        public ItemSpaces(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.right = space / 2;
            outRect.left = space / 2;
            outRect.top = space;
        }
    }
}
